using System;
using System.Windows.Forms;
using RdfMusic;

namespace Mp3ToRdf
{
    public partial class Form1 : Form
    {
        private MusicStore store = new MusicStore();

        public Form1()
        {
            InitializeComponent();
        }

        private void scan_Click(object sender, EventArgs e)
        {
            if (!ReadyToScan())
            {
                DisplayErrorMessage();
                return;
            }
            store.InitialiseStore(txtTo.Text);
            FileScanner fs = new FileScanner();
            fs.newFileScanned += HandleNewFileScannedEvent;
            fs.ScanFiles(txtFrom.Text, store);
            store.WriteStoreToFile(txtTo.Text);
        }

        public void HandleNewFileScannedEvent(object sender, FileScanner.NewFileScannedEventArgs args)
        {
            if (args != null)
                listScanned.Items.Add(args.FileLocation);
        }

        private void DisplayErrorMessage()
        {
            MessageBox.Show("You need to select a location to scan and a place to store the results.", "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
                            MessageBoxOptions.ServiceNotification);
        }

        private bool ReadyToScan()
        {
            return !(txtFrom.Text == null || txtFrom.Text.Length == 0 || txtFrom.Text == "where are the MP3s?");
        }

        private void browseFrom_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                txtFrom.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void browseTo_Click(object sender, EventArgs e)
        {
            DialogResult dr = saveFileDialog1.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                txtTo.Text = saveFileDialog1.FileName;
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (store == null && ReadyToScan())
                store.InitialiseStore(txtTo.Text);
            Form2 f2 = new Form2(store);
            f2.Show(this);
        }
    }
}